package top.soulblack.purchasesale.mapper;


import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import top.soulblack.purchasesale.model.Permission;
import top.soulblack.purchasesale.model.Role;
import top.soulblack.purchasesale.model.User;

import java.util.List;

/**
 * @Author: 韦溜溜
 * @Description:用户表Mapper
 * @Date: Create in 14:25 2019/5/19
 */
@Mapper
@Component
public interface UserMapper{


    @Results(id = "userMap",
            value = {
            @Result(column = "id",property = "userId"),
            @Result(column = "account",property = "account"),
            @Result(column = "name",property = "userName"),
            @Result(column = "password",property = "password"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "role_id",property = "roleId"),
    })
    @Select("select *from user where role_id = #{roleId}")
    List<User> selectByRoleId(@Param("roleId")Integer roleId);

    @ResultMap("userMap")
    @Select("select user.*,shop.name as shop from user left join shop on user.shop_id = shop.id")
    List<User> selectList();

    int deleteByPrimaryKey(Integer id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);

    @Insert("insert into user (id, shop_id,loadtime,ip,creatime,status, account, name, password, phone, email, role_id, openid )" +
            " values (#{userId}, #{shopId}, #{loadtime}, #{ip}, #{creatime}, #{status},#{account}, #{userName}, #{password},#{phone}, #{email}, #{roleId}, #{phone})")
    @Options(useGeneratedKeys = true, keyProperty = "userId",keyColumn = "userId")
    int addUser(User User);

    @Select("select * from user where `shop` = #{shop} order by sort")
    List<User> selectForshop(@Param("shop") String shop);

    @Select("select count(*) from user where account = #{account}")
    int existUser(String account);

    @Select("select * from user where account = #{account}")
    User findByUserName(String account);

    @Delete("delete from user where id = #{id} ")
    int deleteUser(Integer id);

    @Update("update user " +
            "set account=#{account}, name=#{userName}, password=#{password}, phone=#{phone}, email=#{email}, role_id=#{roleId}, " +
            "shop_id=#{shopId},loadtime=#{loadtime},ip=#{ip},creatime=#{creatime},status=#{status} " +
            "where id = #{userId}")
    int updateUser(User user);

    @Select("SELECT *FROM `user` where `user`.openid = #{openid}")
    User selectByOpenid(@Param("openid") String openid);

    /*UserRoleMapper*/
    @Select("select r.id, r.name, r.memo  from role r " +
            "left join users_roles ur on(r.id = ur.role_id)" +
            " left join user  u on(u.id = ur.user_id) where u.account = #{account}")
    List<Role> r_findByUserName(String account);

    /*UserPermissionMapper*/
    @Select("select p.id,p.url,p.name from role r " +
            "        left join users_roles ur on(r.id = ur.role_id)  " +
            "        left join user u on(u.id = ur.user_id) " +
            "        left join roles_permissions rp on(rp.role_id = r.id)  " +
            "        left join permissions p on(p.id = rp.permission_id ) " +
            "        where u.account = #{account}")
    List<Permission> p_findByUserName(String account);
}